#!/usr/bin/env bash
# 更新ETCD相关证书
export HOST0=192.168.1.23
export HOST1=192.168.1.24
export HOST2=192.168.1.25
systemctl stop kubelet
systemctl stop docker

USER=root
for HOST in ${HOST1} ${HOST2}
do
ssh ${USER}@${HOST} 'systemctl stop kubelet'
ssh ${USER}@${HOST} 'systemctl stop docker'
done
mv /etc/kubernetes/pki /etc/kubernetes/pkibak

kubeadm init phase certs etcd-ca
kubeadm init phase certs etcd-server --config=/tmp/${HOST0}/kubeadmcfg.yaml
kubeadm init phase certs etcd-peer --config=/tmp/${HOST0}/kubeadmcfg.yaml
kubeadm init phase certs etcd-healthcheck-client --config=/tmp/${HOST0}/kubeadmcfg.yaml
kubeadm init phase certs apiserver-etcd-client --config=/tmp/${HOST0}/kubeadmcfg.yaml
for HOST in ${HOST1} ${HOST2}
do
scp -r /etc/kubernetes/pki ${USER}@${HOST}:/etc/kubernetes/
ssh ${USER}@${HOST} 'mv /var/lib/kubelet/pki /var/lib/kubelet/pkibak'
done
systemctl start docker
systemctl start kubelet
for HOST in ${HOST1} ${HOST2}
do
ssh ${USER}@${HOST} 'systemctl start kubelet'
ssh ${USER}@${HOST} 'systemctl start docker'

done
sleep 10

docker run --rm -it \
--net host \
-v /etc/kubernetes:/etc/kubernetes registry.aliyuncs.com/google_containers/etcd:3.2.24 etcdctl \
--cert-file /etc/kubernetes/pki/etcd/peer.crt \
--key-file /etc/kubernetes/pki/etcd/peer.key \
--ca-file /etc/kubernetes/pki/etcd/ca.crt \
--endpoints https://${HOST0}:2379 cluster-health

